package appfuse.framework.database;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;

/**
 * 数据操作类
 * <p>Title: 数据操作类</p>
 * <p>Description: 对JDBC进行封装，用于数据操作的类</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company:</p>
 * @author savage
 * @version 1.0
 */
public class SqlCommand2 {
	private Connection conn = null;

	//private Statement stmt = null;

	private PreparedStatement prepstmt = null;

	/**
	 * 初始化
	 */
	public SqlCommand2() {
		try {
			getDataSource();
			//stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	/*
	 * @Functionname:SQLCommand @Describe: 初始化PreparedStatement ��ʼ����ݿ�
	 * @param:sql sql��� @return:
	 */
	public SqlCommand2(String sql) {
		try {
			getDataSource();
			//stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);
			prepareStatement(sql);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	public void setSqlCommand(String sql){
		try {
			prepareStatement(sql);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	/**
	 * 得到数据源
	 */
	private void getDataSource() throws SqlExecException {
		//数据源采用硬编码方式 
		/*try { 
			//加载驱动程序，下面的代码为加载MySQL驱动程序
		  Class.forName("com.mysql.jdbc.Driver"); 
		  //注册MySQL驱动程序
		  DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		  //用适当的驱动程序连接到数据库 
		  String dbUrl = "jdbc:mysql://localhost:3306/crc1?useUnicode=true&characterEncoding=UTF-8";

		  String dbUser="root"; 
		  String dbPwd="12345678"; 
		  //建立数据库连接 
		  conn = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);

		  //非自动提交
		  if (conn.getAutoCommit()) {
		  	conn.setAutoCommit(false);
		  }
		} catch (Exception e) { 
			e.printStackTrace(); 
			throw new SqlExecException("getDataSourceTest() error: " + e.getStackTrace()); 
		}*/

		//数据源采用JNDI
		/*try {
			Context ctx = new InitialContext();
			if (ctx == null) {
				throw new Exception("Boom - No Context");
			}

			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/appfuse");
			if (ds != null) {
				conn = ds.getConnection();
			}

			//非自动提交
			if (conn.getAutoCommit()) {
				conn.setAutoCommit(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SqlExecException("getDataSource() error: "
					+ e.getStackTrace());
		}*/

		//数据源采用Proxool
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			conn = DriverManager.getConnection("proxool.yyfarm");

			//非自动提交
			if (conn.getAutoCommit()) {
				conn.setAutoCommit(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SqlExecException("getDataSource() error: "
					+ e.getStackTrace());
		}
	}

	public Connection getConnection() {
		return conn;
	}

	public void prepareStatement(String sql) throws SQLException {
		prepstmt = conn.prepareStatement(sql);
	}

	public void setString(int index, String value) throws SQLException {
		prepstmt.setString(index, value);
	}

	public void setInt(int index, int value) throws SQLException {
		prepstmt.setInt(index, value);
	}

	public void setBoolean(int index, boolean value) throws SQLException {
		prepstmt.setBoolean(index, value);
	}

	public void setDate(int index, Date value) throws SQLException {
		prepstmt.setDate(index, value);
	}

	public void setTime(int index, Time value) throws SQLException {
		prepstmt.setTime(index, value);
	}

	public void setTimestamp(int index, Timestamp value) throws SQLException {
		prepstmt.setTimestamp(index, value);
	}

	public void setLong(int index, long value) throws SQLException {
		prepstmt.setLong(index, value);
	}

	public void setFloat(int index, float value) throws SQLException {
		prepstmt.setFloat(index, value);
	}
	
	public void setDouble(int index, Double value) throws SQLException {
		prepstmt.setDouble(index, value);
	}

	public void setBinaryStream(int index, InputStream in, int length)
	throws SQLException {
		prepstmt.setBinaryStream(index, in, length);
	}

	public void clearParameters() throws SQLException {
		prepstmt.clearParameters();
	}

	public PreparedStatement getPreparedStatement() {
		return prepstmt;
	}

	/**
	 * 执行查询
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ResultSet executeQuery() throws SQLException {
		if (prepstmt != null) {
			return prepstmt.executeQuery();
		} else {
			return null;
		}
	}

	/**
	 * ִ执行更新
	 * 
	 * @throws SQLException
	 */
	public void executeUpdate() throws SQLException {
		if (prepstmt != null) {
			prepstmt.executeUpdate();
		}
	}

	/**
	 * 提交
	 */
	public void commit() throws SQLException {
		conn.commit();
	}

	/**
	 * 回滚
	 */
	public void rollback() throws SQLException {
		conn.rollback();
	}

	/**
	 * 关闭
	 */
	public void close() {
		try {
			if (prepstmt != null) {
				prepstmt.close();
				prepstmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	//得到自增长Id
	public int getGeneratedKeys(){
		int autoIncKeyFromApi = -1;

		ResultSet rs;
		try {
			rs = prepstmt.getGeneratedKeys();
			if (rs.next()) {
				autoIncKeyFromApi = rs.getInt(1);
			
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return autoIncKeyFromApi;
	}


}